package com.sms.web.domain.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sms.domain.entity.Application;
import com.sms.domain.entity.SmsHistory;
import com.sms.domain.service.ApplicationService;
import com.sms.domain.service.SmsHistoryService;

/**
 * @desc 短信发送历史记录
 * @author WuJiangWu
 * @date 2018年7月21日上午1:15:20
 */
@Controller
@RequestMapping(value = "/admin/history")
public class SmsHistoryController {
    
    protected static Logger logger = LoggerFactory.getLogger(SmsHistoryController.class);
    
    /** template服务 */
    @Autowired
    private SmsHistoryService smsHistoryService;
    
    /** template服务 */
    @Autowired
    private ApplicationService applicationService;
    
    /**
     * 配置信息列表
     * @param queryLike
     * @param applicationId
     * @param startTime
     * @param endTime
     * @param sendResult
     * @param pageNumber
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping(value = "/list")
    public String pageList(String queryLike, String applicationId, Date startTime, Date endTime, String sendResult,
        @RequestParam(defaultValue = "1") int pageNumber, @RequestParam(defaultValue = "20") int pageSize, ModelMap model) {
        Map<String, Object> filterApplication = new HashMap<>();
        Map<String, Object> filterHistory = new HashMap<>();
        
        filterApplication.put("EQ_delFlag", "0");
        if (StringUtils.isNotBlank(applicationId)) {
            filterHistory.put("EQ_application.applicationId", applicationId);
        }
        
        if (StringUtils.isNotBlank(queryLike)) {
            filterHistory.put("LIKE_content", queryLike);
        }
        
        if (startTime != null) {
            filterHistory.put("GTE_sendTime", startTime);
        }
        
        if (endTime != null) {
            filterHistory.put("LT_sendTime", DateUtils.addDays(endTime, 1));
        }
        
        if (StringUtils.isNotBlank(sendResult)) {
            filterHistory.put("EQ_sendResult", sendResult);
        }
        
        Page<SmsHistory> list = smsHistoryService.findAll(filterHistory, pageNumber, pageSize);
        model.put("data", list);
        
        List<Application> applications = applicationService.findAll(filterApplication);
        model.put("applications", applications);
        return "sms/smslist";
    }
    
}
